library(SDMTools)

input.dir='E:SEABat/new/overlap/';setwd(input.dir)

files=list.files(pattern='overlap.asc')

richness=read.asc.gz(file)
for (tfile in files) {cat(tfile, '\n')
	overlap=read.asc.gz(tfile)
	overlap[which(overlap<3)]=0  #set values less than 3 to zero
	overlap[which(overlap==3)]=1  #set values equal to 3 (where current and future overlap) to 1
	
	if (is.null(richness)) {richness = overlap;  } #define the output & set everything = 0
	richness = richness + overlap
	}
legend.pnts = cbind(c(93,91,91,93),c(2,2,7.5,7.5))
cols = c("gray", colorRampPalette(c("#CC9966","forestgreen","darkgreen","#003300"))(max(richness,na.rm=T))) #get the colors for the plot
png('future_overlap_richness.png', width=nrow(richness),height=ncol(richness), units='px', res=300, pointsize=5, bg='white') #start the plot
	par(mar=c(0,0,0,0)+0.1) #remove any plot margins
	image(richness, ann=FALSE,axes=FALSE,col=cols, zlim=range(richness,na.rm=T)) #plot the richness
	legend.gradient(legend.pnts,cols=cols,limits=range(richness,na.rm=T), title='Richness', cex=2)
dev.off()
